package cn.micro.eye.commons.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.*;

import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Micro Entity
 *
 * @param <T>
 * @author lry
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class MicroEntity<T extends Model> extends Model<T> {

    public static final String ID_FIELD = "id";
    public static final String CREATED_AT_FIELD = "created_at";
    public static final String CREATED_BY_FIELD = "created_by";
    public static final String UPDATED_AT_FIELD = "updated_at";
    public static final String UPDATED_BY_FIELD = "updated_by";
    public static final String DELETED_FIELD = "deleted";

    public static final String CREATED_AT_NAME = "createdAt";
    public static final String CREATED_BY_NAME = "createdBy";
    public static final String UPDATED_AT_NAME = "updatedAt";
    public static final String UPDATED_BY_NAME = "updatedBy";
    public static final String DELETED_NAME = "deleted";

    @TableId(type = IdType.AUTO)
    @JSONField(serializeUsing = ToStringSerializer.class)
    protected Long id;

    /**
     * The created time
     */
    @TableField(fill = FieldFill.INSERT)
    protected Timestamp createdAt;

    /**
     * The creator(id/ip/etc)
     */
    @TableField(fill = FieldFill.INSERT)
    protected String createdBy;

    /**
     * The updated time
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Timestamp updatedAt;

    /**
     * The updater(id/ip/etc)
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected String updatedBy;

    /**
     * Optimistic lock
     * @Version
     * private Long version;
     */

    /**
     * {@link cn.micro.eye.type.DeletedEnum}
     */
    @TableLogic
    @TableField(fill = FieldFill.INSERT)
    protected Integer deleted;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}